package mn.more.wits.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import mn.more.wits.client.dto.Payload;
import mn.more.wits.client.dto.UserDTO;
import mn.more.wits.client.exception.AlreadyInSessionException;
import mn.more.wits.client.exception.WitsSecurityException;
import mn.more.wits.client.util.ClientModelUtil;
import mn.more.wits.client.util.FormatUtil;
import net.mygwt.ui.client.Style;

/**
 * @author $Author: mikeliucc $
 * @version $Revision: 119 $
 */
class AuthCallback implements AsyncCallback {

	private static final AppMsg APP_MSG = AppMsg.Self.get();
	private static final String TITLE = "Authentication";

	private RecoverSessionCallback recoverSessionCallback = new RecoverSessionCallback();

	private static class RecoverSessionCallback implements AsyncCallback {

		public void onFailure(Throwable caught) {
			AppContext.notifyCloseApp(TITLE, APP_MSG.authMissingUserContext(), APP_MSG.serverNonGwtHomePage());
		}

		public void onSuccess(Object result) {
			if (result != null) {
				Payload payload = (Payload) result;
				UserDTO user = new UserDTO();
				ClientModelUtil.toModel(payload, user);
				// save user for later reference.
				AppContext.setUser(user);
				AppContext.showStartPage();
			}
		}
	}

	public void onFailure(Throwable e) {
		AppContext.hideLoading();
		if (e instanceof WitsSecurityException) {
			String message;
			int errorCode = ((WitsSecurityException) e).getErrorCode();
			switch (errorCode) {
				case WitsSecurityException.ERROR_ACCOUNT_DISABLED:
				case WitsSecurityException.ERROR_ACCOUNT_LOCKED_OUT:
					message = APP_MSG.authAccountInactive();
					break;
				case WitsSecurityException.ERROR_ALREADY_IN_SESSION:
					message = APP_MSG.authAlreadyInSession(((AlreadyInSessionException) e).getInSessionId());
					break;
				case WitsSecurityException.ERROR_ALREADY_LOGGED_IN:
					if (AppContext.getUser() == null) {
						AuthProvider.App.getInstance().getAuth(recoverSessionCallback);
					} else {
						AppContext.showStartPage();
					}
					return;
				case WitsSecurityException.ERROR_GENERAL:
				default:
					message = APP_MSG.authFailed();
					break;
			}

			if (message != null) { AppContext.notifyCloseApp(TITLE, message, APP_MSG.serverNonGwtHomePage()); }
		} else {
			AppContext.notifyCloseApp(TITLE, e.getMessage(), APP_MSG.serverNonGwtHomePage());
		}
	}

	public void onSuccess(Object result) {
		AppContext.hideLoading();
		if (result == null) {
			AppContext.setStatusError(APP_MSG.authFailed());
		} else {
			//Info.show("AuthCallback", "result is " + result, "");

			Payload payload = (Payload) result;
			UserDTO user = new UserDTO();
			ClientModelUtil.toModel(payload, user);

			// todo: remove debug
			AppContext.showMessageBox(TITLE, Style.ICON_INFO, Style.OK, "login successful");

			// save user for later reference.
			AppContext.setUser(user);
			String loginDate = FormatUtil.formatLongDate(user.getLoginDate());
			String name = user.getDisplayName();
			AppContext.setStatusMessage(APP_MSG.loginImg(), APP_MSG.authSuccess(name, loginDate));
			AppContext.showStartPage();
		}
	}
}
